package ar.com.utn.openqsb.main;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.SwingUtilities;

import ar.com.utn.openqsb.commons.Module;

/**
 * An action listener to initialize the {@link Module} when the user click a
 * button in the main window.
 * 
 * @author rvega
 * 
 */
public class LauncherActionListener implements ActionListener {

	private String classToLaunch;

	public LauncherActionListener(final String classToLaunch) {
		this.classToLaunch = classToLaunch;
	}

	public void actionPerformed(ActionEvent e) {

		SwingUtilities.invokeLater(new Runnable() {
			@SuppressWarnings("unchecked")
			public void run() {
				try {
					Class module = Class.forName(classToLaunch);
					Module moduleFrame = (Module) module.newInstance();
					moduleFrame.prepare();
					moduleFrame.launch();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});

	}

}
